#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

import abc

from informix.comm.common_models import ActionResult, SubJobDetails


class MetaService(metaclass=abc.ABCMeta):
    def __init__(self, job_manager, param_dict):
        self.job_manager = job_manager
        self.param_dict = param_dict
        self.log_detail = {}

    @abc.abstractmethod
    def get_steps(self):
        """
        接口服务的任务详细步骤
        :return list
        """
        pass

    @abc.abstractmethod
    def gen_param_obj(self):
        """
        生成对应接口服务的参数对象
        """
        pass


class MetaServiceWorker(metaclass=abc.ABCMeta):
    """
    备份、恢复、资源接入功能实现基类，
    接收任务对象、参数对象和命令对象为入参，
    任务基本步骤功能实现，只关注任务业务功能实现
    基本功能函数通过
        调用update_action_result  返回action结果
        调用update_report_result  返回job_detail结果
    """

    def __init__(self, job_manager, param_obj, cmd_obj=None):
        self.job_manager = job_manager
        self.param = param_obj
        self.cmd = cmd_obj
        self.return_result = None
        self.interface_steps = {}

    def update_result(self):
        """
        更新功能函数执行结果，并调用job_manager.update更新任务结果状态
        :param : 功能函数执行结果结构体返回
        :return: None
        """
        self.job_manager.update(self.return_result)

    def update_action_result(self, code, body_err_code=0, msg="", err_param=None):
        """

        :param code: action 执行结果code
        :param body_err_code: 错误码
        :param msg: 执行作息
        :param err_param: 错误参数
        :return:
        """
        self.return_result = ActionResult(code=code, body_err=body_err_code, message=msg, body_err_params=err_param)
        self.update_result()

    def update_report_result(self, status, progress, log_detail=None):
        """

        :param status: 任务状态
        :param progress:任务进度
        :param log_detail:上报label结构体
        :return:
        """
        self.return_result = SubJobDetails(
            task_id=self.job_manager.job_id,
            progress=progress,
            task_status=status,
            log_detail=[log_detail]
        )
        self.update_result()

    def set_result(self, restlt):
        self.return_result = restlt


class MetaInterface(metaclass=abc.ABCMeta):
    """
    接口公共属性混合类
    need_report: 接口任务是否支持上报进度
    log_mag: 接口任务日志标签字典{0(任务状态)：(label_dict){log_info:"",log_info_param:[],log_level:number,……}}
    """
    need_report = False
    log_map = {}


class SupportResourceInterfaceMixin(MetaInterface):
    pass


class CheckApplicationInterfaceMixin(MetaInterface):
    pass


class RemoveProtectInterfaceMixin(MetaInterface):
    pass
